package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import com.intershop.beehive.app.capi.App;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.internal.app.AppContextUtil;
import com.intershop.sellside.appbase.b2c.capi.basket.B2CBasketBO;
import com.intershop.sellside.appbase.b2c.capi.basket.PLISplitInfo;
import com.intershop.sellside.appbase.b2c.capi.basket.PLISplitInfoMap;
import com.intershop.sellside.appbase.b2c.capi.basket.PLISplitInfoSplitStrategy;
import com.intershop.sellside.appbase.b2c.capi.order.ProductLineItemBO;

/**
 * This pipelet returns the iterator of all the split line items
 */
public class CreatePLISplitInfoIterator extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'CartBO'
     */
    public static final String DN_CART_BO = "CartBO";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'PLISplitInfoIterator'
     */
    public static final String DN_PLI_SPLIT_INFO_ITERATOR = "PLISplitInfoIterator";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'StoreAddressIDMap'
     */
    public static final String DN_STORE_ADDRESS_IDMAP = "StoreAddressIDMap";
    
    /**
     * Constant for the shipping PLI split info strategy.
     */
    public static final String SHIPPING_PLI_SPLIT_INFO_STRATEGY = "ShippingPLISplitInfoSplitStrategy";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException
    {
        // lookup 'CartBO' in pipeline dictionary
        B2CBasketBO cartBO = (B2CBasketBO)dict.get(DN_CART_BO);
        if (null == cartBO)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'CartBO' not available in pipeline dictionary.");

        }
        // ProductLineItemBO productLineItemBO;
        Iterator<ProductLineItemBO> productLineItemBOIterator = null;
        Iterator<PLISplitInfo> tempPLISplitInfoIterator = null;
        Iterator<PLISplitInfo> pLISplitInfoIterator = null;
        Collection<PLISplitInfo> pLISplitInfos;
        Vector<PLISplitInfo> pLISplitInfoVector = new Vector<PLISplitInfo>();

        // get map of split product line item from cart BO
        PLISplitInfoSplitStrategy splitStrategy = null;
        App app = AppContextUtil.getCurrentAppContext().getApp();
        
        if (app != null)
        {
            splitStrategy = app.getNamedObject(SHIPPING_PLI_SPLIT_INFO_STRATEGY);
        }
        
        PLISplitInfoMap pLISplitInfoMap = cartBO.getPLISplitInfoMap(splitStrategy);
        if (pLISplitInfoMap != null)
        {
            productLineItemBOIterator = pLISplitInfoMap.keySet().iterator();
        }
        if (productLineItemBOIterator != null)
        {
            while(productLineItemBOIterator.hasNext())
            {
                ProductLineItemBO productLineItemBO = (ProductLineItemBO)productLineItemBOIterator.next();
                pLISplitInfos = pLISplitInfoMap.get(productLineItemBO);
                tempPLISplitInfoIterator = pLISplitInfos.iterator();
                if (tempPLISplitInfoIterator != null)
                    while(tempPLISplitInfoIterator.hasNext())
                    {
                        PLISplitInfo pLISplitInfo = (PLISplitInfo)tempPLISplitInfoIterator.next();
                        pLISplitInfoVector.add(pLISplitInfo);
                    }
            }
        }

        pLISplitInfoIterator = pLISplitInfoVector.iterator();
        // store 'SplitLineItemIterator' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_PLI_SPLIT_INFO_ITERATOR, pLISplitInfoIterator);
        return PIPELET_NEXT;
    }
}